﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CPPEI.PM.Web
{
    public partial class InitDatabase : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitSys();
            InitMD();
            InitPM();
        }

        private void InitSys()
        {
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["pm"].ConnectionString;

            SqlConnection connection = new SqlConnection(connectionString);
            try
            {
                connection.Open();

                string sqlText = File.ReadAllText(Server.MapPath("~/Sql/SysManagement.sql.txt"));
                SqlCommand command = new SqlCommand(sqlText, connection);

                int resultCount = command.ExecuteNonQuery();

                Response.Write("<b>Success!</b>");
            }
            catch (Exception ex)
            {
                Response.Write(string.Format("<b>Failure!</b><br/><em>{0}</em>", ex));
            }
            finally
            {
                if (connection.State == System.Data.ConnectionState.Closed)
                {
                    connection.Close();
                }
            }
        }

        private void InitMD()
        {
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["pm"].ConnectionString;

            SqlConnection connection = new SqlConnection(connectionString);
            try
            {
                connection.Open();

                string sqlText = File.ReadAllText(Server.MapPath("~/Sql/MultiDimensional.sql.txt"));
                SqlCommand command = new SqlCommand(sqlText, connection);

                int resultCount = command.ExecuteNonQuery();

                Response.Write("<b>Success!</b>");
            }
            catch (Exception ex)
            {
                Response.Write(string.Format("<b>Failure!</b><br/><em>{0}</em>", ex));
            }
            finally
            {
                if (connection.State == System.Data.ConnectionState.Closed)
                {
                    connection.Close();
                }
            }
        }

        private void InitPM()
        {
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["pm"].ConnectionString;

            SqlConnection connection = new SqlConnection(connectionString);
            try
            {
                connection.Open();

                string sqlText = File.ReadAllText(Server.MapPath("~/Sql/ProjectManagement.sql.txt"));
                SqlCommand command = new SqlCommand(sqlText, connection);

                int resultCount = command.ExecuteNonQuery();

                Response.Write("<b>Success!</b>");
            }
            catch (Exception ex)
            {
                Response.Write(string.Format("<b>Failure!</b><br/><em>{0}</em>", ex));
            }
            finally
            {
                if (connection.State == System.Data.ConnectionState.Closed)
                {
                    connection.Close();
                }
            }
        }
    }
}